package com.yonyou.pmclouds.basecom.runtime;

import com.yonyou.pmclouds.basecom.multilanguage.Language;

/**
 * 请求端、服务端公用的环境，线程级别
 */
public class RuntimeEnvironment {
    public static final String KEY_DATA_SOURCE = "ds";
    public static final String KEY_LANG = "lang";
    public static final String KEY_COUNTRY = "country";
    public static final String KEY_LOGIN_USER = "loginUser";
    public static final String KEY_TENANT_ID = "tenantId";
    public static final String KEY_SYS_ID = "sysid";
    private static ThreadLocal<EnvironmentObject> threadLocal = new ThreadLocal<EnvironmentObject>() {
        protected EnvironmentObject initialValue() {
            return new EnvironmentObject();
        }
    };

    public static void setLanguage(Language lan) {
        threadLocal.get().langId = lan;
    }

    public static Language getLanguage() {
        Language lan = threadLocal.get().langId;
        return lan;
    }

    public static void setDataSource(String ds) {
        threadLocal.get().datasource = ds;
    }

    public static String getDataSource() {
        return threadLocal.get().datasource;
    }

    public static String getLoginUserId() {
        return threadLocal.get().userId;
    }

    public static void setLoginUserId(String id) {
        threadLocal.get().userId = id;
    }

    public static String getTenantId() {
        return threadLocal.get().tenantId;
    }

    public static void setTenantId(String tenantId) {
        threadLocal.get().tenantId = tenantId;
    }

    public static String getSysId() {
        return threadLocal.get().sysid;
    }

    public static void setSysId(String sysId) {
        threadLocal.get().sysid = sysId;
    }

    public static void resetCurrentThread() {
        threadLocal.get().reset();
    }

    static class EnvironmentObject {
        private String userId;
        private String tenantId;
        private String sysid;
        private String datasource;
        private Language langId;

        protected void reset() {
            userId = null;
            tenantId = null;
            sysid = null;
            datasource = null;
            langId = null;
        }
    }
}